package com.tydic.product.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tydic.common.core.enums.CarNumberAllowEnum;
import com.tydic.product.domain.CarNumber;
import com.tydic.product.mapper.CarNumberMapper;
import com.tydic.product.service.ICarNumberService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class CarNumberServiceImpl implements ICarNumberService {
    private final CarNumberMapper carNumberMapper;

    @Override
    public List<CarNumber> findByBusinessIdAndBusinessTypeAndCreateDate(String businessId, String businessType, String createDate) {
        return carNumberMapper.selectList(Wrappers.lambdaQuery(CarNumber.class).eq(CarNumber::getBusinessId, businessId).eq(CarNumber::getBusinessType, businessType).eq(CarNumber::getCreateDate, createDate));
    }

    @Override
    public List<CarNumber> findByBusinessIdAndBusinessType(String businessId, String businessType) {
        return carNumberMapper.selectList(Wrappers.lambdaQuery(CarNumber.class).eq(CarNumber::getBusinessId, businessId).eq(CarNumber::getBusinessType, businessType));
    }

    @Override
    public CarNumber findByCarNumberAndCreateDate(String carNumber, String createDate) {
        return carNumberMapper.selectOne(Wrappers.lambdaQuery(CarNumber.class).eq(CarNumber::getCarNumber, carNumber).eq(CarNumber::getCreateDate, createDate), false);
    }

    @Override
    public CarNumber findByBusinessIdAndBusinessTypeAndCarNumberAndCreateDate(String businessId, String businessType, String carNumber, String createDate) {
        return carNumberMapper.selectOne(Wrappers.lambdaQuery(CarNumber.class).eq(CarNumber::getBusinessId, businessId).eq(CarNumber::getBusinessType, businessType).eq(CarNumber::getCarNumber, carNumber).eq(CarNumber::getCreateDate, createDate), false);
    }

    @Override
    public Page<CarNumber> findByBusinessIdAndBusinessType(Page<CarNumber> page, String businessId, String businessType) {
        return carNumberMapper.selectPage(page, Wrappers.lambdaQuery(CarNumber.class).eq(CarNumber::getBusinessId, businessId).eq(CarNumber::getBusinessType, businessType));
    }

    @Override
    public Page<CarNumber> findByCarNumber(Page<CarNumber> page, String carNumber) {
        return carNumberMapper.selectPage(page, Wrappers.lambdaQuery(CarNumber.class).eq(CarNumber::getCarNumber, carNumber));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteByBusinessIdAndBusinessType(String businessId, String businessType) {
        return carNumberMapper.delete(Wrappers.lambdaQuery(CarNumber.class).eq(CarNumber::getBusinessId, businessId).eq(CarNumber::getBusinessType, businessType));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(String businessId, String businessType, String carNumber) {
        String today = DateUtil.today();
        CarNumber entity = findByBusinessIdAndBusinessTypeAndCarNumberAndCreateDate(businessId, businessType, carNumber, today);
        if (entity != null) {
            return;
        }
        entity = new CarNumber();
        entity.setCarNumber(carNumber);
        entity.setBusinessId(businessId);
        entity.setBusinessType(businessType);
        carNumberMapper.insertOrUpdate(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(String businessId, String businessType, List<String> carNumbers) {
        for (String carNumber : carNumbers) {
            save(businessId, businessType, carNumber);
        }
    }

    @Override
    public Page<CarNumber> page(int pageNo, int pageSize, String businessId, String businessType) {
        return findByBusinessIdAndBusinessType(new Page<>(pageNo, pageSize), businessId, businessType);
    }

    @Override
    public Page<CarNumber> page(int pageNo, int pageSize, String carNumber) {
        return findByCarNumber(new Page<>(pageNo, pageSize), carNumber);
    }

    @Override
    public Page<CarNumber> page(int pageNo, int pageSize) {
        LambdaQueryWrapper<CarNumber> lqw = Wrappers.lambdaQuery(CarNumber.class).orderByDesc(CarNumber::getCreateDate);
        return carNumberMapper.selectPage(new Page<>(pageNo, pageSize), lqw);
    }

    @Override
    public List<CarNumber> list(String businessId, String businessType, String createDate) {
        return findByBusinessIdAndBusinessTypeAndCreateDate(businessId, businessType, createDate);
    }

    @Override
    public List<CarNumber> list(String businessId, String businessType) {
        return findByBusinessIdAndBusinessType(businessId, businessType);
    }

    @Override
    public CarNumber get(String carNumber, String createDate) {
        return findByCarNumberAndCreateDate(carNumber, createDate);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void clear(String businessId, String businessType) {
        deleteByBusinessIdAndBusinessType(businessId, businessType);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateAllow(String businessId, String businessType) {
        List<CarNumber> carNumbers = findByBusinessIdAndBusinessType(businessId, businessType);
        if (CollUtil.isNotEmpty(carNumbers)) {
            for (CarNumber carNumber : carNumbers) {
                carNumber.setAllow(CarNumberAllowEnum.YES.getAllow());
                carNumberMapper.insertOrUpdate(carNumber);
            }
        }
    }
}
